<?php
namespace Api\Controller;
use Api\Controller\CommonController;
class MessageController extends CommonController {
	public function _initialize(){
		parent::_initialize();
		 
		//登录验证
		//$this->_checkLogin();
	}
	
	/**
	 *  消息列表 
	 */
	public function messageList(){
		empty($_POST['page'])?$page=1:$page=intval(I("post.page"));
		empty($_POST['pageSize'])?$pageSize=15:$pageSize=intval(I("post.pageSize"));
		$type = intval($_POST['type']);
		empty($type)?$type=1:$type=intval(I('post.type'));
		
		isset($_POST['categoryID'])?$categoryID =intval($_POST['categoryID']):$categoryID=0;
		isset($_POST['regionID'])?$regionID=intval($_POST['regionID']):$regionID=0;
		
		$where=array(
			"isOn"=>1,
			//"isSend"=>1,
			'messageType'=>$type
		);
		
		if($categoryID!=0){
			$where['categoryID'] = $categoryID;
		}
		if($regionID!=0){
			$m_r = M('Message_region')->where(array('regionID'=>$regionID))->select();
			foreach($m_r AS $key=>$value){
				if($value!=end($m_r)){
					$message .= $value['messageID'] . ',';
				} else {
					$message .= $value['messageID'];
				}
			}
			$where['messageID'] =  array('in',$message);
		}
		
		
		$isIndex = intval($_POST['isIndex']);
		!empty($isIndex)?$where['isShow']=1:'';
		
		$order=array(
			'index'=>'ASC',
			"messageID"=>"desc"
		);
		
		$count=D("Message")->getMessageListLength($where);
		$pageInfo=$this->paging($count,$page,$pageSize);
    	$field="messageID,title,author,messageType,url,img,img2,messageType,jumpType,goodsID,shopID,shopType,people,intro,content,materialID,categoryID";
		$list=D("Message")->getMessageList($page, $pageSize, $where, $order,$field);
		
		foreach ($list as $key=>$a){
			if(!empty($a['img'])){
				$list[$key]['img']=C("IMG_DOMAIN").$a['img'];
			}
			if(!empty($a['img2'])){
				$list[$key]['img2']=C("IMG_DOMAIN").$a['img2'];
			}
			$img = M('Category')->where(array('categoryID'=>$a['categoryID']))->getField('img');
			$list[$key]['categoryLogo'] = C('IMG_DOMAIN').$img;
			if($a['jumpType']=='material'){
				$list[$key]['url'] = C('SERVER_DOMAIN')."/CNEPShop/Material/view.html?id=".$a['materialID'];
			}

		}
		//var_dump($list);
		$this->_returnJson(array("result"=>1,"list"=>$list,"page"=>$pageInfo,"lastID"=>$list[0]['messageID']));
	}
    
	
	/**
	 *  获取消息条数
	 */
	public function getMessageNum(){
		
		isset($_POST['messageType'])?$messageType =intval($_POST['messageType']):$messageType=1;
		
		if($_POST['lastID']!=0){
			empty($_POST['lastID'])?$this->_returnJson(array("result"=>0,"info"=>"missing parameter","code"=>"123")):$lastID=intval(I("post.lastID"));
		} else {
			$lastID = 0;
		}
		
		
		
		$where=array(
				"messageType"=>$messageType,
				"isOn"=>1,
				//"isSend"=>1,
				"messageID"=>array("gt",$lastID)
		);
		
		$num=D("Message")->where($where)->count();
		
		//获取最新ID
		$order=array(
				"messageID"=>"desc"
		);
		
		unset($where['messageID']);
		$newLastID=D("Message")->where($where)->order($order)->getField("messageID");
		
		$this->_returnJson(array("result"=>1,"num"=>$num,"lastID"=>$newLastID));
	}
	
	/**
	 *  获取消息详情
	 */
	public function messageDetail(){
		$data=array(
			"messageID"=>intval(I("post.messageID"))
		);
		if(!isNotEmptyArr($data)) $this->_returnJson(array("result"=>0,"info"=>"missing parameter","code"=>"123"));
		
		$isMessage=D("Message")->getMessage($data['messageID'],array("isOn"=>1));
		if(!$isMessage) $this->_returnJson(array("result"=>0,"info"=>"message is not exist","code"=>"126"));
		$isMessage['content']=urlencode($isMessage['content']);
		if($isMessage['jumpType']=='material'){
			$isMessage['url'] = C('SERVER_DOMAIN')."/CNEPShop/Material/view.html?id=".$isMessage['materialID'];
		}
		$this->_returnJson(array("result"=>1,"info"=>$isMessage,"url"=>U("Home/Index/messageDetailMsg",array("messageID"=>$data['messageID'])),"host"=>getHost()));
	}

}